package models

import (
	"fmt"
	"github.com/jinzhu/gorm"
)

type Note struct {
	gorm.Model
	Key string `gorm:"unque:not null"`
	UserID int
	User User
	Title string `gorm:"type:varchar(200)"`
	Summary string  `gorm:"type:varchar(800)"`
	Content string `gorm:"type:text"`
	Visit int   `gorm:"default:0"`
    Praise int  `gorm:"default:0"`
}

func QueryNoteByKey(key string)(note Note,err error){
	return note,db.Debug().Where("`key`=?",key).Take(&note).Error
}

func QueryNoteByKeyAndUseid(key string,userid int)(note Note,err error){
	return note,db.Where("`key`=? and user_id=?",key,userid).Take(&note).Error
}

func SaveNote(note *Note) error{
	return db.Debug().Save(note).Error
}

func QueryNotesByPage(title string,page,limit int)(note []*Note,err error){
	return note ,db.Where("title like ?",fmt.Sprintf("%%%s%%",title)).Order("updated_at desc").Offset((page-1)*limit).Limit(limit).Find(&note).Error
}

func QueryNotesCount(title string)(count int,err error){
	return count,db.Model(&(Note{})).Where("title like ?",fmt.Sprintf("%%%s%%",title)).Count(&count).Error
}

func DeleteNoteByUserIDAndKey(key string,userid int) error{
	return db.Delete(&Note{},"`key`=? and user_id=?",key,userid).Error
}